﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

    @RenderPage("CommonHead.cshtml")
    <title>@ViewBag.Title</title>
    @RenderSection("Head", required: false)
</head>
<body class="easyui-layout">
    <div data-options="region:'center',border:false" style="padding: 1px">
        <div class="easyui-layout" fit="true" border="false">
            <div region="center" border="false">
                <div id="grid">
                </div>
            </div>

            @RenderBody()
        </div>
    </div>
</body>
</html>

<script type="text/javascript">
    // 全局变量
    var grid;
    var dlg_Edit;
    var dlg_Edit_form;
    var searchWin;
    var searchForm;
    var virpath = ""; //网站的虚拟目录 如：/ShopManager
    var config = {
 
        AddActionURL: '@ViewBag.AddActionURL',
        DelActionURL: '@ViewBag.DelActionURL',
        EditActionURL: '@ViewBag.EditActionURL',
        SelectActionURL:'@ViewBag.SelectActionURL',
        ListActionURL:'@ViewBag.ListActionURL',
        GridOption:{
            title: '@ViewBag.Title',
            iconCls: 'icon-save',
            methord: 'post',
            url: '@ViewBag.ListActionURL',
            method: 'post',
            sortName: '@ViewBag.ListSortName',
            sortOrder: '@ViewBag.ListSortOrder',
            idField: '@ViewBag.IdColumn',
            pageSize: 30,
            striped: true,
            queryParams:null,// { BookCategoryCode: 'Root' }
            frozenColumns: [[
                { field: 'ck', checkbox: true }

            ]],
            columns: null,
            fit: true,
            pagination: true,
            rownumbers: true,
            fitColumns: true,
            singleSelect: false,
            toolbar: [{
                text: '新增',
                iconCls: 'icon-add',
                handler: add
            }, '-', {
                text: '修改',
                iconCls: 'icon-edit',
                handler: edit
            }, '-', {
                text: '删除',
                iconCls: 'icon-remove',
                handler: del
            }, '-', {
                text: '查找',
                iconCls: 'icon-search',
                handler: OpensearchWin
            }], onDblClickRow: function (rowIndex, rowData) {  //双击事件
                dlg_Edit.dialog('open');
                dlg_Edit_form.form('clear');
                dlg_Edit.dialog('setTitle', '您正在修改:' + rowData.@ViewBag.NameColumn);
                    //alert(dlg_Edit.LoadDataAfterEvent);
                    dlg_Edit_form.form('load', rowData); //加载到表单的控件上      
                    if (dlg_Edit.LoadDataAfterEvent) {
                        dlg_Edit.LoadDataAfterEvent(rowData);
                    }
                    
                    dlg_Edit_form.url = virpath + config.EditActionURL + '?id=' + rowData.@ViewBag.IdColumn;

                }, onHeaderContextMenu: function (e, field) {
                    e.preventDefault();
                    if (!$('#tmenu').length) {
                        createColumnMenu();
                    }
                    $('#tmenu').menu('show', {
                        left: e.pageX,
                        top: e.pageY
                    });
                }

        }
    };

        $(document).ready(function () {

            grid = $('#grid').datagrid(config.GridOption);

            dlg_Edit = $('#Dlg-Edit').dialog({
                closed: true,
                modal: true,
                toolbar: [{
                    text: '保存',
                    iconCls: 'icon-save',
                    handler: saveData
                }, '-', {
                    text: '关闭',
                    iconCls: 'icon-no',
                    handler: function () {
                        dlg_Edit.dialog('close');
                    }
                }]

            });
            dlg_Edit_form = dlg_Edit.find('form');
 

            $('#btn-search,#btn-search-cancel').linkbutton();
            searchWin = $('#search-window').window({
                iconCls: 'icon-search',
                closed: true,
                modal: true
            });
            searchForm = searchWin.find('form');

            $('body').layout();

        });


        function createColumnMenu() {
            var tmenu = $('<div id="tmenu" style="width:100px;"></div>').appendTo('body');
            var fields = grid.datagrid('getColumnFields');
            for (var i = 0; i < fields.length; i++) {
                $('<div iconCls="icon-ok"/>').html(fields[i]).appendTo(tmenu);
            }
            tmenu.menu({
                onClick: function (item) {
                    if (item.iconCls == 'icon-ok') {
                        grid.datagrid('hideColumn', item.text);
                        tmenu.menu('setIcon', {
                            target: item.target,
                            iconCls: 'icon-empty'
                        });
                    } else {
                        grid.datagrid('showColumn', item.text);
                        tmenu.menu('setIcon', {
                            target: item.target,
                            iconCls: 'icon-ok'
                        });
                    }
                }
            });
        }

        function getSelectedArr() {
            var ids = [];
            var rows = grid.datagrid('getSelections');
            for (var i = 0; i < rows.length; i++) {
                ids.push(rows[i].@ViewBag.IdColumn);
        }
        return ids;
    }
    function getSelectedID() {
        var ids = getSelectedArr();
        return ids.join(',');
    }
    function arr2str(arr) {
        return arr.join(',');
    }

    function add() {
        dlg_Edit_form.form('clear');
        dlg_Edit.dialog('open');
        dlg_Edit.dialog('setTitle', '@ViewBag.AddWindowTitle');
        $("#@ViewBag.IdColumn").val(NewGuid());

        dlg_Edit_form.url = virpath + config.AddActionURL;
        //Msgalert("提示", '成功调用', "info");    
    }
    function edit() {
        var rows = grid.datagrid('getSelections');
       
        var num = rows.length;
        if (num == 0) {
            Msgshow('请选择一条记录进行操作!');
            //$.messager.alert('提示', '请选择一条记录进行操作!', 'question');       
            return;
        }
        else if (num > 1) {
            Msgfade('您选择了多条记录,只能选择一条记录进行修改!'); //$.messager.alert('提示', '您选择了多条记录,只能选择一条记录进行修改!', 'info');
            return;
        }
        else {
            dlg_Edit_form.form('clear');
            dlg_Edit.dialog('open');
            dlg_Edit.dialog('setTitle', '您正在修改' + rows[0].@ViewBag.IdColumn);
            dlg_Edit_form.form('load', rows[0]); //加载到表单的控件上 
            
            $("#OrderDate").datebox("setValue", Common.DateFormatter(rows[0].OrderDate));
            dlg_Edit_form.url = virpath + config.EditActionURL + '?id=' + rows[0].@ViewBag.IdColumn;

        }
}
    function del() {
    var arr = getSelectedArr();
    //alert(arr);
    if (arr.length > 0) {
        $.messager.confirm('提示信息', '您确认要删除选中的所有记录吗?', function (data) {
            if (data) {
                $.ajax({
                    url: virpath + config.DelActionURL + '?ids=' + arr2str(arr),
                    type: 'post',
                    error: function () {
                        Msgalert('错误', '删除失败!', 'error');
                        grid.datagrid('clearSelections');
                    },
                    success: function (Result) {
                        var data = eval('(' + Result + ')');
                        if (data.success) {
                            Msgfade(arr.length + "条记录" + data.msg); //提示消息
                            grid.datagrid('reload');
                            grid.datagrid('clearSelections');
                        } else {
                            Msgalert('错误', data.msg, 'error');
                        }
                    }
                });
            }
        });
    } else {
        Msgshow('请先选择要删除的记录。');

    }
}

function OpensearchWin() {
    searchWin.window('open');
    searchForm.form('clear');
}

function saveData() {
    //alert(dlg_Edit_form.url);
    dlg_Edit_form.form('submit', {
        url: dlg_Edit_form.url,
        onSubmit: function () {
            //alert(dlg_Edit_form.form('validate'))
            if (dlg_Edit.SubmitBeforeLoad) {
                dlg_Edit.SubmitBeforeLoad();
            }
            return dlg_Edit_form.form('validate');
        },
        onLoadSuccess:function() {
                
        },
        onBeforeLoad:function() {
              
        },
        success: function(result) {
            successCallback(result);
        }   //~/Content/JqueryUI/MyEasyUIcommmon.js
    });
}


function showAll() {
    grid.datagrid({ url: virpath + config.ListActionURL });
}
function OpensearchWin() {
    searchWin.window('open');
    searchForm.form('clear');
}

function SearchOK() {
      
    searchWin.window('close');
    // alert( config.ListActionURL)

    grid.datagrid({ url: virpath + config.ListActionURL+"?"+searchForm.serialize() });
}
function closeSearchWindow() {
    searchWin.window('close');
}
    
</script>

@RenderSection("Script", required: false)

